package com.qf.cloud2204.microb.controller;

import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/hello")
public class HelloController {
    @Autowired
    EurekaClient eurekaClient;
    @Autowired
    RestTemplate restTemplate;
    @GetMapping("/test1")
    public String test1(){

        //调用微服务A的接口: get请求 /a/hello

        //根据服务的名字，获取服务的ip地址和端口号
        InstanceInfo instanceInfo = eurekaClient.getNextServerFromEureka("MICROA", false);
        String homePageUrl = instanceInfo.getHomePageUrl();
        String hostName = instanceInfo.getHostName();
        String ipAddr = instanceInfo.getIPAddr();
        int port = instanceInfo.getPort();
        //组织请求的url
        String url = homePageUrl+"a/hello";
        //发送http请求冰接受响应数据
        String result = restTemplate.getForObject(url, String.class);
        System.out.println(result);
        return result;
    }
}
